package net.oschina.dajiangnan.wx.process.pay;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import net.oschina.dajiangnan.util.XStreamHelper;
import net.oschina.dajiangnan.wx.util.RandomUtil;
import net.oschina.dajiangnan.wx.util.Signature;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import net.oschina.dajiangnan.wx.constants.Config;
import net.oschina.dajiangnan.wx.constants.Custom;
import net.oschina.dajiangnan.wx.process.pay.bean.Product;
import net.oschina.dajiangnan.wx.process.pay.tools.Unifiedorder;
import net.oschina.dajiangnan.wx.process.pay.tools.Unifiedorder.Result;
import net.oschina.dajiangnan.wx.process.pay.xmlbean.ScanQrcodeNotifyReq;
import net.oschina.dajiangnan.wx.process.pay.xmlbean.ScanQrcodeNotifyResp;
import net.oschina.dajiangnan.wx.qr.Qrcode;


public class Scan_qrcode_notify {

    private static Logger logger = Logger.getLogger(Scan_qrcode_notify.class);

    
    
    public String process(HttpServletRequest req) {
        
        try{
            InputStream reqBody = req.getInputStream();
            String xmlStr = IOUtils.toString(reqBody);
            ScanQrcodeNotifyReq scanQrcodeNotifyReq = XStreamHelper.toBean(xmlStr, ScanQrcodeNotifyReq.class);
            logger.debug("微信支付系统向扫码回调地址post内容如下:\r\n" + scanQrcodeNotifyReq);
            
            if(!scanQrcodeNotifyReq.islegalSign(Config.apikey)){
                throw new Scan_qrcode_notify.ScanQrcodeNotifyReqUnlegalSign();
            }
            
            Product product = Qrcode.getProduct(scanQrcodeNotifyReq.getProduct_id());
            if(product == null){
                return "can not get product by productid = " + scanQrcodeNotifyReq.getProduct_id();
            }
            Result result = Unifiedorder.get(req, scanQrcodeNotifyReq.getOpenid(), product,Unifiedorder.TradeType.NATIVE);
            
            
            ScanQrcodeNotifyResp scanQrcodeNotifyResp = getScanQrcodeNotifyResp(result.getPrepayid());
            logger.debug("扫码回调地址给微信支付系统返回内容如下:\r\n" + scanQrcodeNotifyResp);
            

            Custom.wx.scanQrcodeForPayListener(scanQrcodeNotifyReq.getOpenid(),scanQrcodeNotifyReq.getProduct_id(),scanQrcodeNotifyResp.getPrepay_id(),result.getOrderid());
            
            return scanQrcodeNotifyResp.toString();
            
        }catch(Unifiedorder.UnifiedorderRespUnlegalSign e){
            
            ScanQrcodeNotifyResp scanQrcodeNotifyResp = new ScanQrcodeNotifyResp();
            scanQrcodeNotifyResp.setReturn_code("FAIL");
            scanQrcodeNotifyResp.setReturn_msg("unifiedorderResp unlegalSign");
            
            logger.debug("unifiedorderResp签名验证失败,扫码回调地址给微信支付系统返回内容如下:\r\n" + scanQrcodeNotifyResp);
            
            return scanQrcodeNotifyResp.toString();
        }catch(Scan_qrcode_notify.ScanQrcodeNotifyReqUnlegalSign e){
            
            ScanQrcodeNotifyResp scanQrcodeNotifyResp = new ScanQrcodeNotifyResp();
            scanQrcodeNotifyResp.setReturn_code("FAIL");
            scanQrcodeNotifyResp.setReturn_msg("scanQrcodeNotifyReq unlegalSign");
            
            logger.debug("scanQrcodeNotifyReq签名验证失败,扫码回调地址给微信支付系统返回内容如下:\r\n" + scanQrcodeNotifyResp);
            
            return scanQrcodeNotifyResp.toString();
        }catch(Throwable e){
            e.printStackTrace();
        }
        return "";
        
        
    }
    
    
    
    private ScanQrcodeNotifyResp getScanQrcodeNotifyResp(String prepay_id) throws IllegalAccessException{
        ScanQrcodeNotifyResp scanQrcodeNotifyResp = new ScanQrcodeNotifyResp();
        scanQrcodeNotifyResp.setReturn_code("SUCCESS");
        scanQrcodeNotifyResp.setReturn_msg("");
        scanQrcodeNotifyResp.setAppid(Config.appid);
        scanQrcodeNotifyResp.setMch_id(Config.mch_id);
        scanQrcodeNotifyResp.setNonce_str(RandomUtil.getRandomStr(32));
        scanQrcodeNotifyResp.setPrepay_id(prepay_id);
        scanQrcodeNotifyResp.setResult_code("SUCCESS");
        scanQrcodeNotifyResp.setErr_code_des("");
        scanQrcodeNotifyResp.setSign(Signature.getSign(scanQrcodeNotifyResp, null,Config.apikey));
        return scanQrcodeNotifyResp;
    }
    public static class ScanQrcodeNotifyReqUnlegalSign extends Exception{

        private static final long serialVersionUID = -8421501635712387504L;
        public ScanQrcodeNotifyReqUnlegalSign(){
            super();
        }
        public ScanQrcodeNotifyReqUnlegalSign(String message){
            super(message);
        }
    }
}
